<template>
	<view>
		<cu-custom :bgColor="globalData.bgColor" :isBack="true">
			<template #content>
				<text class="text-darkgrey">{{ $t('account.index.6s54p7') }}</text>
			</template>
		</cu-custom>
		<view class="cu-list menu sm-border solid-top">
			<view class="cu-item arrow" @click="avatarClick">
				<view class="content">
					<text class="cuIcon-pic text-darkgreyv"></text>
					<text class="text-darkgrey">{{ $t('account.index.q5sc29') }}</text>
				</view>
				<view class="action">
					<view class="cu-avatar round df" :style="[{ backgroundImage: 'url(' + (form.avatar ? form.avatar : '') + ')' }]"></view>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-people text-darkgrey"></text>
					<text class="text-darkgrey">{{ $t('account.index.37001l') }}</text>
				</view>
				<view class="action">
					<text class="text-purple-grey text-df">{{ form.username }}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-mobile text-darkgrey"></text>
					<text class="text-darkgrey">{{ $t('account.index.r0ewak') }}</text>
				</view>
				<view class="action">
					<text class="text-purple-grey text-df">{{ form.phone }}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-mail text-darkgrey"></text>
					<text class="text-darkgrey">{{ $t('account.index.5g2u01') }}</text>
				</view>
				<view class="action text-right">
					<input v-if="isEdit" :placeholder="$t('account.index.46mi73')" name="email" v-model="form.email" />
					<text v-else class="text-purple-grey text-df">{{ form.email ? form.email : '' }}</text>
				</view>
			</view>
		</view>
		<view class="text-right padding">
			<navigator url="reset-password" class="text-blue text-df">{{ $t('account.index.hc2hw6') }}</navigator>
		</view>
	</view>
</template>

<script>
import JimUtil from '@/utils/jim-util';
import service from '@/store/service'; //存储相关数据服务util
import __config from '@/config/env'; // 配置文件
import { editObj, bindPhone, getObj } from '@/api/upms/user';

export default {
	onLoad() {
		this.form = service.getUser();
	},
	onShow() {
		this.getUserInfo();
	},
	data() {
		return {
			globalData: getApp().globalData,
			value: '',
			isEdit: false, //是否是编辑状态
			bannerShow: false,
			userInfo: {},
			form: {}
		};
	},

	methods: {
		editClick() {
			this.isEdit = !this.isEdit;
			uni.setNavigationBarTitle({
				title: this.$t('account.index.lb8xvm')
			});
		},
		cancelClick() {
			this.isEdit = !this.isEdit;
			uni.setNavigationBarTitle({
				title: this.$t('account.index.6s54p7')
			});
		},
		saveClick() {
			var that = this;
		},
		avatarClick() {
			//选择图片
			var that = this;
			uni.chooseImage({
				count: 1,
				success: (res) => {
					if (res) {
						let filePaths = res.tempFilePaths[0];
						uni.uploadFile({
							url: __config.basePath + '/upms/file/upload?fileType=image&dir=user/',
							filePath: filePaths,
							name: 'file',
							header: {
								Authorization: 'Bearer ' + that.globalData.access_token
							},
							success: (result) => {
								try {
									if (result.statusCode === 200) {
										let resultObject = JSON.parse(result.data);
										that.form.avatar = resultObject.link;
										that.submitUserInfo();
									} else {
										uni.showToast({
											title: this.$t('account.index.x8td0b'),
											icon: 'none'
										});
									}
								} catch (e) {
									console.log(e);
								}
							},
							fail: (err) => {
								try {
									uni.showToast({
										title: this.$t('account.index.cyc9e5') + err,
										icon: 'none'
									});
								} catch (e) {
									console.log(e);
								}
								console.log('upload file err: ' + JSON.stringify(err));
							}
						});
					}
				}
			});
		},
		submitUserInfo() {
			editObj(this.form)
				.then((response) => {
					service.saveUserInfo(this.form);
					//更新一下im个人资料
					JimUtil.updateIMuserInfo(this.form);
					uni.showToast({
						title: this.$t('order.detail.m6f5u2')
					});
				})
				.catch(() => {
					uni.showToast({
						icon: 'none',
						title: this.$t('account.index.s1ro1w')
					});
				});
		},
		getUserInfo() {
			var that = this;
		}
	}
};
</script>

<style></style>
